const network = require('../../utils/network.js')
const loginUtil = require('../../utils/login.js')
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  tempModel: null,
  cardID:null,
  data: {
    userInfo: null,
    //添加车型
    addModel: false,
    models: [],
    index: 0,
    show: false,
    items: [],
    modelCell: {
      name: '车型'
    },
    tempModel: null,
    mainActiveIndex: 0,
    activeId: 0,
    //我的车型 至多展示一个
    myCarModel:{
      models:[],
      showManager:true
    },
    //是否编辑了 手机号 身份证  地址
    editFlag:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadModels();
  },

  // 加载所有车型
  loadModels: function() {
    network.getRequest('mp/service/base/app/models', '', res => {
      const models = res.data;
      this.setData({
        items: res.data,
      })
      this.refreshModelUI();
    })
  },

  refreshModelUI: function() {
    const models = this.data.items;
    if (models.length > 0) {
      const activeModel = models[this.data.mainActiveIndex];
      let activeItem = null;
      for (let item of activeModel.children) {
        if (item.id === this.data.activeId) {
          activeItem = item
        }
      }

      if (activeItem == null) return;
      let modelCell = Object.assign({}, { ...this.data.modelCell
      }, {
        desc: activeModel.text + '/' + activeItem.text
      })
      this.setData({
        modelCell: modelCell
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.loadUserInfo();
  },

  loadUserInfo:function(){
    const loginInfo = loginUtil.loadLoginInfo();
    network.getRequest('mp/service/user/info', 'accesstoken=' + loginInfo.accessToken, (res) => {
      const info = res.data ;
      app.globalData.userInfo = info ;
      const models = info.models ;
      if(models.length>0){
        this.setData({
          userInfo: res.data,
          myCarModel:{
            models: [models[0]],
            showManager: models.length>1
          }
        })
      }
      else{
        this.setData({
          userInfo: res.data
        })
      }
     
      //我的车型


    }, err => { })
  },

  addModel: function() {
    // 保存
    if(this.data.addModel){
      const {sn} = loginUtil.loadLoginInfo();
      const params = {
        "modelId": this.data.activeId,
        "carNumber": this.cardID,
        "userSn": sn
      }
    
      network.requestLoading('mp/service/user/addUserCarModel', params, '请求中', (res) => {
        this.loadUserInfo();
        this.setData({
          models: [],
          addModel: false
        })
      })
    }
    else{
      this.setData({
        models: [1],
        addModel: true
      })
    }
  },

  // 弹出车型选择
  itemClick: function() {
    this.setData({
      show: true
    })
  },

  onClickNav({
    detail = {}
  }) {
    console.log(detail);
    this.setData({
      mainActiveIndex: detail.index || 0
    });
  },

  onClickItem({
    detail = {}
  }) {
    this.setData({
      activeId: detail.id,
    });
    this.tempModel = detail;
    console.log(detail);
  },

  onClose: function() {
    this.setData({
      show: false
    })
  },

  onConfirm: function() {
    this.onClose();
    this.refreshModelUI();
  },

// 车牌号输入
  onCardIDChange:function(e){
    this.cardID = e.detail ;
  },

  carManager:function(){
    wx.navigateTo({
      url: '/pages/me/car-manager',
    })
  },

// 保存用户信息
  saveUserInfo:function(){
    if(!this.data.editFlag)return;
    const userInfo = this.data.userInfo;

    if (userInfo.nickName == undefined) {
      wx.showToast({
        title: '请输入姓名',
      })
      return;
    }

    if (userInfo.idCard==undefined){
      wx.showToast({
        title: '请输入身份证号',
      })
      return ;
    }

    if (userInfo.idCard.length<15) {
      wx.showToast({
        title: '请输入合法身份证号',
      })
      return;
    }


    const params = {
      "nickName": userInfo.nickName,
      "address": userInfo.address,
      "idCard": userInfo.idCard
    }
    network.requestLoading('mp/service/user/edit', params, '请求中', (res) => {
      this.loadUserInfo();
      this.setData({
        editFlag:false
      })
      wx.showToast({
        title: '已更新',
      })
    })
  },

  nameInput:function(e){
    const value = e.detail ;
    const userInfo = this.data.userInfo ;
    this.setData({
      editFlag: true,
      userInfo:Object.assign({},userInfo,{
        nickName: value,
      })
    })

  },

  idCardInput:function(e){
    const value = e.detail;
    const userInfo = this.data.userInfo;

    this.setData({
      editFlag: true,
      userInfo: Object.assign({}, userInfo, {
        idCard: value,
      })
    })
  },

  addressInput:function(e){
    const value = e.detail;
    const userInfo = this.data.userInfo;

    this.setData({
      editFlag: true,
      userInfo: Object.assign({}, userInfo, {
        address: value,
      })
    })
  }
})